/*
@title: Humpty Dumpty
@description: "Humpty Dumpty" is an endless runner game where players help Humpty jump over walls to prevent him from falling again. The game requires quick reflexes as one wrong move ends the run. Aim to get Humpty as far as possible without crashing into obstacles.
@author: Arnav R
@tags: []
@addedOn: 2024-08-23
*/

const player = "p";
const wallTop = "t";
const wallBottom = "w";
const ground1 = "g";
const ground2 = "h";
const dead = "d";

setLegend(
  [player, bitmap`
................
..999999966666..
..999999966666..
..999999996666..
..999009900666..
..999009900666..
..999999999666..
..999999999666..
..999999999966..
...9999999996...
...9999999996...
...9999999999...
...9999999999...
...999FFFF999...
99999FFFFFF99999
99999FFFFFF99999`],
  [dead, bitmap`
................
................
................
................
.....11.111.....
.....111111.....
.......11.......
.......11.......
99999999999999FF
99999999999999FF
99999999999999FF
99999999999.9999
.99999999999.99.
..999999999...9.
................
................`],
  [wallTop, bitmap`
................
....777777......
....777777......
....777777......
....777777......
....777777......
....777777......
....777777......
....777777......
....777777......
....777777......
....777777......
....777777......
....777777......
....777777......
................`],
  [wallBottom, bitmap`
................
....777777......
....777777......
....777777......
....777777......
....777777......
....777777......
....777777......
....777777......
....777777......
....777777......
....777777......
....777777......
....777777......
....777777......
................`],
  [ground1, bitmap`
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC
................
................
................
................
................
................
................
................
................
................`],
  [ground2, bitmap`
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC
................
................
................
................
................
................
................
................
................
................`]
);

const levels = [
  map`
..............................
..............................
..............................
..............................
..............................
..............................
..............................
..............................
...p..........................
hghghghghghghghghghghghghghghg`,
];

setMap(levels[0]);

addText("Humpty Runner", { 
  x: 3,
  y: 5,
  color: color`0`,
});

addText("By @Arnav", { 
  x: 5,
  y: 6,
  color: color`3`,
});

addText("Press W to Start", { 
  x: 3,
  y: 8,
  color: color`6`,
});

let started = 0;
onInput("w", () => {
  if (started == 0) {
    started = 1;
    start();
    clearText();
  } else if (started == -1) {
    clearText();
    started = 0;
    setMap(levels[0]);
    addText("Press W to start", { 
      x: 2,
      y: 6,
    });
  }
  jump();
});

function start() {
  let moveWalls, addWalls;
  let score = 0;
  moveWalls = setInterval(function() {
    clearText();
    score += 1;
    getAll(wallTop).forEach(function(e) {
      e.x -= 1;
      clearTile(0, 8);
    });
    getAll(wallBottom).forEach(function(e) {
      e.x -= 1;
      clearTile(0, 8);
    });
    if (getTile(3, 8).length > 1 || getTile(3, 7).length > 1) {
      started = -1;
      clearInterval(moveWalls);
      clearInterval(addWalls);
      clearTile(3, 8);
      clearTile(3, 7);
      addSprite(3, 8, dead);
      addText("womp womp", { 
        x: 0,
        y: 6,
      });
    }
    addText("" + score, { 
      x: 1,
      y: 5,
    });
  }, 100);
  
  addWalls = setInterval(function() {
    setTimeout(function() {
      let wall = Math.floor(Math.random() * (4 - 1) + 1);
      if (wall == 1 || wall == 2) {
        addSprite(29, 8, wallTop);
      } else {
        addSprite(29, 8, wallBottom);
        addSprite(29, 7, wallTop);
      }
    }, Math.floor(Math.random() * (700 - 1) + 1));
  }, 1200);
}

function jump() {
  getFirst(player).y = 7;
  setTimeout(function() {
    getFirst(player).y = 6;
  }, 100);
  setTimeout(function() {
    getFirst(player).y = 7;
  }, 300);
  setTimeout(function() {
    getFirst(player).y = 8;
  }, 400);
}

